<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
Route::group(['before' => 'auth'], function() {
	Route::get('product/componentsSuggestions', 'ProductController@getComponentsSuggestions');
	Route::get('order/viewDetails/{id}', 'OrderController@viewDetails');
	Route::post('order/executedList', 'OrderController@getExecutedList');
	Route::post('product/retrieveComponents', 'ProductController@postRetrieveComponents');

	Route::controller('dashboard', 'DashboardController');
	Route::controller('orderComponent', 'OrderComponentController');
	Route::controller('loggedUser', 'LoggedUserController');

	// admin
	Route::group(['before' => 'admin'], function() {
		Route::post('user/list', 'UserController@getList');
		Route::resource('user', 'UserController');
	});
	
	// cadastro
	Route::group(['before' => 'crud'], function() {
		Route::post('order/list', 'OrderController@getList');
		Route::post('customer/list', 'CustomerController@getList');
		Route::post('product/list', 'ProductController@getList');
		Route::get('product/create/{fromId?}', 'ProductController@create');
		Route::get('order/getByOrderNumber/{orderNumber}', 'OrderController@getByOrderNumber');
		
		Route::resource('customer', 'CustomerController');
		Route::resource('order', 'OrderController');
		Route::resource('product', 'ProductController');
	});
});

Route::get('/', 'LoginController@getIndex');
Route::controller('login', 'LoginController');

// TODO: remover antes de ir pra produção
// causa erro de concorrencia ao tentar gravar arquivo ao mesmo tempo
if (Config::get('database.log', false))
{
	Event::listen('illuminate.query', function($query, $bindings, $time, $name)
	{
		$data = compact('bindings', 'time', 'name');

		// Format binding data for sql insertion
		foreach ($bindings as $i => $binding)
		{
			if ($binding instanceof \DateTime)
			{
				$bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
			}
			else if (is_string($binding))
			{
				$bindings[$i] = "'$binding'";
			}
		}

		// Insert bindings into query
		$query = str_replace(array('%', '?'), array('%%', '%s'), $query);
		$query = vsprintf($query, $bindings);

		Log::info($query, $data);
	});
}